<template>
  <div class="home-container">
    <!-- 顶部搜索导航栏区域 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 频道列表区域 -->
    <!-- 通过 v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签。 -->
    <!-- 通过 animated 属性可以开启切换标签内容时的转场动画。 -->
    <!-- 通过 swipeable 属性可以开启滑动切换标签页。 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 文章列表 -->
        <article-list :channel="channel"></article-list>
      </van-tab>
      <!-- 自定义插槽 -->
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="ischannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="ischannelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <channel-edit
        :my-channels="channels"
        :active="active"
        @update-cative="onUpdateActive"
      >
      </channel-edit>
    </van-popup>
  </div>
</template> 

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit,
  },
  data() {
    return {
      active: 0,
      // 频道列表数据
      channels: [],
      // 控制弹出层的显示与隐藏
      ischannelEditShow: false,
    }
  },
  computed: {
    ...mapState(['user']),
  },
  created() {
    this.loadChannels()
  },
  methods: {
    // 获取频道数据
    async loadChannels() {
      try {
        let channels = []
        // const { data: res } = await getUserChannels()
        // this.channels = res.data.channels
        // console.log(this.channels)
        // console.log(res.data)
        if (this.user) {
          // 已登录
          const { data: res } = await getUserChannels()
          channels = res.data.channels
        } else {
          // 未登录
          const loadChannels = getItem('TOUTIAO_CHANNELS')
          if (loadChannels) {
            channels = loadChannels
          } else {
            // 没有的情况下
            const { data: res } = await getUserChannels()
            channels = res.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道数据失败')
      }
    },
    onUpdateActive(index, ischannelEditShow = true) {
      // 更新激活的频道
      this.active = index
      // 关闭弹出层的显示
      this.ischannelEditShow = ischannelEditShow
    },
  },
}
</script>

<style lang="less">
.home-container {
  padding-top: 90px;
  padding-bottom: 100px;
  .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 320px;
    height: 32px;
    background-color: #5babfb;
    border: none;
    font-size: 16px;
    .van-icon {
      font-size: 20px;
    }
  }
  .channel-tabs {
    .van-tab__warp {
      height: 82px !important;
    }
  }
  .van-tab {
    min-width: 95px;
    border-right: 1px solid #edeff3 !important;
    font-size: 16px;
    color: #777777;
  }
  .van-tab--active {
    color: #333333;
  }
  .van-tabs__line {
    width: 31px !important;
    height: 4px;
    background-color: #3296fa;
    bottom: 20px;
    border-radius: 5px;
  }
  .van-tabs__wrap--scrollable {
    top: 46px;
    position: fixed;
    z-index: 1;
    left: 0;
    right: 0;
  }
  .hamburger-btn {
    position: fixed;
    display: flex;
    justify-content: center;
    align-items: center;
    right: 0;
    width: 40px;
    height: 44px;
    background-color: #fff;
    opacity: 0.902;
    .toutiao {
      font-size: 20px !important;
      color: #323232;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url('~@/assets/gradient-gray-line.png');
      background-size: contain;
    }
  }
  .placeholder {
    flex-shrink: 0;
    height: 40px;
    width: 30px;
    // background-color: red;
  }
}
</style>

